package de.breitbeil.jbooking.ui.view;

import de.breitbeil.jbooking.domain.Person;
import de.breitbeil.jbooking.ui.dataview.DataView;
import de.breitbeil.jbooking.ui.dataview.columns.DataViewColumnImpl;
import de.breitbeil.jbooking.ui.dataview.columns.StringPropertyDataViewColumn;
import de.breitbeil.jbooking.ui.form.PersonForm;
import de.breitbeil.jbooking.ui.view.tablecellrenderer.ConcatenateTableCellRenderer;

public class PersonView extends DataView {

    public final static String PAGE_ID = "personView";
    
    public PersonView() throws InstantiationException, IllegalAccessException {
        super(PAGE_ID, Person.class, PersonForm.class);
    }
    
    public void init() {
        addColumn(new StringPropertyDataViewColumn("lastname"));
        addColumn(new StringPropertyDataViewColumn("firstname"));
        addColumn(createAdressColumn());
        addColumn(new StringPropertyDataViewColumn("email"));
        
        addExportColumn("id");
        addExportColumn("firstname");
        addExportColumn("lastname");
        addExportColumn("address1");
        addExportColumn("address2");
        addExportColumn("address3");
        addExportColumn("zip");
        addExportColumn("city");
        addExportColumn("homePhone");
        addExportColumn("workPhone");
        addExportColumn("email");
        addExportColumn("description");
    }

    protected DataViewColumnImpl createAdressColumn() {
        String[] properties = new String[]{ "address1", "address2", "address3" };
        ConcatenateTableCellRenderer renderer = new ConcatenateTableCellRenderer(Person.class, properties, "<br>");
        renderer.addProperty("zip", " ");
        renderer.addProperty("city", "");
        
        DataViewColumnImpl column = new DataViewColumnImpl("address");
        column.setCellRenderer(renderer);
        column.setFilterProperties(new String[]{"address1", "address2", "address3", "zip", "city"} );
        column.setObjectCellRenderer(true);
        return column;
    }
    
    /* Only for Java >= 1.6
    @Override
    protected CommandGroup createPopupCommands() {
        EmailExecutor emailExecutor = new EmailExecutor();
        getCommandConfigurer().configure(emailExecutor);
        ValueModel selectionHolder = new ListSelectionValueModelAdapter(getDataViewTable().getSelectionModel());
        new ListSingleSelectionGuard(selectionHolder, emailExecutor);
        
        CommandGroup popup = super.createPopupCommands();
        popup.addSeparator();
        popup.add(emailExecutor);
        
        return popup;
    }
    
    protected class EmailExecutor extends ActionCommand {
        private Desktop desktop = Desktop.getDesktop();
        
        public EmailExecutor() {
            super("emailCommand");
        }
        
        public void doExecuteCommand() {
            if(desktop.isSupported(Desktop.Action.MAIL)) {
                Object o = getDataViewTable().getSelectedObject();
                Person person = (Person) o;
                
                if(person == null || person.getEmail() == null) {
                    return;
                }
                
                URI uriMailTo = null;
                try {
                    uriMailTo = new URI("mailto", person.getEmail(), null);
                    desktop.mail(uriMailTo);
                }
                catch(Exception ex) {
                    logger.error("Mailto failed", ex);
                }
            }
        }
    }
    */
}

